package com.cainaer.shop.config;

import com.cainaer.shop.domain.vo.VegetableMarketVo;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 *
 *
 * @author 周世雄
 * @version 1.0
 * @date 2020/10/15 14:36
 * @effect
 * 计算配送各种数值的类
 *
 *
 */
public class Calculate {

    /**
     * 根据两点间距离，计算配送所需的时间
     *
     * @param distance
     *           配送距离
     * @return
     */
    public static String calculateDeliveryTime(double distance){

            //计算配送的时间：额外的距离，以每公里5分钟计算,加上标准50分钟
            double deliveryTime1=(distance-5)*5+50;
            int deliveryTime2= Integer.parseInt(new java.text.DecimalFormat("0").format(deliveryTime1));
            int hours = (int) Math.floor(deliveryTime2 / 60);
            int minute = deliveryTime2 % 60;
            if (hours!=0){
                return hours + "小时" + minute + "分钟";
            }
            return minute + "分钟";
    }

    /**
     * 根据条件重新排序
     *
     * @param vegetableMarketVos  List数据
     * @param condition   排序条件0.默认 1.评价 2.销量 3.距离
     *           配送距离
     * @return
     */
    public static void Sort(List<VegetableMarketVo> vegetableMarketVos, Integer condition) {



        Collections.sort(vegetableMarketVos, new Comparator<VegetableMarketVo>() {
            @Override
            public int compare(VegetableMarketVo o1, VegetableMarketVo o2) {

                double diff = o1.getDistance() - o2.getDistance();

                if (condition==1) {
                    diff = o1.getEvaluate() - o2.getEvaluate();
                    }
                if (condition==2) {
                    diff = Double.valueOf(o1.getSalesVolume()) - Double.valueOf(o2.getSalesVolume());
                    if (diff > 0) {
                        diff= -1;
                    } else if (diff < 0) {
                        diff= 1;
                    }
                }
                if (condition==3) {
                    diff = o1.getDistance() - o2.getDistance();
                }


                if (diff > 0) {
                    return 1;
                } else if (diff < 0) {
                    return -1;
                }
                return 0; //相等为0
            }
        });

    }
}
